package plateau;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import entite.Entite;
import entite.EntityProperties;
import entite.Equipe;



public class Plateau {
	private final static int WIDTH = 50;
	private final static int HEIGHT = 20;
	private final static String CASES_PROPERTY = "cases";
	private final static String CASES_SEPARATOR = ";";
	private final static String CASE_IMAGE_PROPERTY = "-image";
	private final static String CASE_JOUABLE_PROPERTY = "-jouable";
	private final static String PLATEAU_PROPERTY = "plateau";
	private static final Object EQUIPE_POSITION_PROPERTY = "equipe-position";
	private static final String CASE_BEGIN_PROPERTY = "-begin";
	private static final Object EQUIPE_PROPERTIES_PROPERTY = "equipe-properties";
	
	private Case[][] m_contenuPlateau = new Case[WIDTH][HEIGHT];
	private Map<String, String> m_properties = new Hashtable<String, String>();
	private Plateau2D m_visualisation;
	private List<Equipe> m_equipes = new ArrayList<Equipe>();
	
	public void load(String propertyFileName) throws IOException, URISyntaxException {
		loadProperties(propertyFileName);
		loadPlateau();
		loadVisualisation();
		loadEquipe();
	}

	private void loadVisualisation() throws IOException, URISyntaxException {
		m_visualisation = new Plateau2D(m_contenuPlateau);
		m_visualisation.setVisible(true);
		m_visualisation.pack();
	}


	private void loadEquipe() throws IOException, URISyntaxException {
		String equipePositionFileName = m_properties.get(EQUIPE_POSITION_PROPERTY);
		String equipePropertiesFileName = m_properties.get(EQUIPE_PROPERTIES_PROPERTY);
		EntityProperties.load(equipePropertiesFileName);
		String[] entityTypes = EntityProperties.getTypes();
		URL url = Plateau.class.getResource(equipePositionFileName);
		FileInputStream fis = new FileInputStream(new File(url.toURI()));
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		int xcursor = 0;
		int ycursor = 0;
		String ligne;
		Equipe equipe0 = new Equipe();
		equipe0.name = "Equipe 0";
		Equipe equipe1 = new Equipe();
		equipe1.name = "Equipe 1";
		m_equipes.add(equipe0);
		m_equipes.add(equipe1);
		while ((ligne = br.readLine())!=null){
			String[] ligneSplit = ligne.split(CASES_SEPARATOR);
			xcursor = 0;
			ycursor++;
			for (String str : ligneSplit) {
				for(String key : entityTypes) {
					if(str.equals(key)) {
						Entite e = new Entite();
						e.m_plateau2D=m_visualisation;
						e.m_plateau=m_contenuPlateau;
						e.m_position= new Position();
						e.m_position.x=xcursor;
						e.m_position.y=ycursor;
						e.m_comportement=EntityProperties.getComportement(key);
						e.resource=new GlobalResources();
						e.resource.setImageFilename(EntityProperties.getImage(key));
						e.key=key;
						Case c = m_contenuPlateau[xcursor][ycursor];
						c.entite=e;
						if(c.begin) {
							equipe0.entities.add(e);
							e.equipeAdverse = equipe1;
						} else {
							equipe1.entities.add(e);
							e.equipeAdverse = equipe0;
						}
						e.m_caracteristique = EntityProperties.getCarac(key);
					}
				}
				xcursor ++;
			}
		}
	}

	private void loadPlateau() throws IOException, URISyntaxException {
		String plateauFileName = m_properties.get(PLATEAU_PROPERTY);
		String[] casesKeys = m_properties.get(CASES_PROPERTY).split(CASES_SEPARATOR);
		URL url = Plateau.class.getResource(plateauFileName);
		FileInputStream fis = new FileInputStream(new File(url.toURI()));
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		int xcursor = 0;
		int ycursor = 0;
		String ligne;
		while ((ligne = br.readLine())!=null){
			String[] cases = ligne.split(CASES_SEPARATOR);
			for (String str : cases) {
				for(String key : casesKeys) {
					if(str.equals(key)) {
						Case c = new Case();
						c.jouable = Boolean.parseBoolean(m_properties.get(key + CASE_JOUABLE_PROPERTY));
						c.resource = new GlobalResources();
						c.resource.setImageFilename(m_properties.get(key + CASE_IMAGE_PROPERTY));
						c.position = new Position();
						c.position.x = xcursor;
						c.position.y = ycursor;
						c.begin = Boolean.parseBoolean(m_properties.get(key+CASE_BEGIN_PROPERTY));
						m_contenuPlateau[xcursor][ycursor] = c;
						xcursor ++;
						if (xcursor >= WIDTH) {
							xcursor = 0;
							ycursor++;
						}
					}
				}
			}
		}
	}

	private void loadProperties(String propertyFileName)
			throws FileNotFoundException, IOException, URISyntaxException {
		URL url = Plateau.class.getResource(propertyFileName);
		FileInputStream fis = new FileInputStream(new File(url.toURI()));
		InputStreamReader isr = new InputStreamReader(fis);
		BufferedReader br = new BufferedReader(isr);
		String ligne;
		while ((ligne=br.readLine())!=null){
			int indexOf = ligne.indexOf("=");
			if(indexOf != -1){
				m_properties.put(ligne.substring(0, indexOf), ligne.substring(indexOf + 1));
				System.out.println(ligne.substring(0, indexOf) + " # " + ligne.substring(indexOf + 1));
			}
		}
		br.close();
	}
	public List<Equipe> getEquipes() {
		return m_equipes;
	}

	public void repaint() {
		m_visualisation.repaint();
		repaintEntites();
	}
	public void repaintEntites() {
		for(Equipe equipe : m_equipes) {
			for(Entite entite : equipe.entities) {
				entite.repaint();
			}
		}
	}
}
